package com.trace.exception;

import com.trace.controller.report.rep.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public ResponseEntity<APIResult> handleException(Throwable e) {
        if (e instanceof MessageException) {
            return new ResponseEntity<>(APIResult.fail(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        log.error(e.getMessage());
        return new ResponseEntity<>(APIResult.fail("服务器处理失败"), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
